<?php
/**
 * 应用系统节点管理
 * @author tangbl
 *
 */
class NodeAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 添加节点
     * @access public
     * @param  int    $pid   父节点
     * @param  string $name  节点标识
     * @param  string $title 节点名称
     * @return array
     */
    public function add(){
    	
    	$param = array(
    		"after"   => "node_deal_after",
    	);
    	parent::do_add("Node",$param);
    }
    
    /**
     * 修改节点
     * @access public
     * @param  int    $pid   父节点
     * @param  string $name  节点标识
     * @param  string $title 节点名称
     * @return array
     */
    public function edit(){
    	 
    	$param = array(
    		"after"   => "node_deal_after",
    	);
    	parent::do_edit("Node",$param);
    }
    
    /**
     * 列出所有节点
     * @access public
     * @param  int    $roleid  角色id
     * @param  int    $level   节点等级
     * @param  string $where   查询条件
     * @return array
     */
    public function index(){
    	    	
    	$level = $this->_get("level","intval",0);
    	if($level){
    		$map['level'] = array('elt',$level);
    	}
    	
    	$model = D("Node");
    	$data  = $model->listNode($map);
    	
    	$this->success($data);
    }
    
    /**
     * 根据对应的条件查询节点
     * @access public
     * @param  int    $id  节点id
     * @return array
     */
    public function find(){
    
    	$id = $this->_get("id","intval",0);
    	 
    	$model = D("Node");
    	$map   = array("id"=>$id);
    	$data  = $model->where($map)->find();
    	 
    	$this->success($data);
    }
    
    /**
     * 删除节点
     * @access public
     * @param  int    $id  节点编号
     * @return array
     */
    public function del(){
    	 
    	$id     = $this->_get("id","intval",0);
    	
    	$model  = D("Node");
    	$result = $model->delNode($id);
    	
    	if($result){
    		$this->success($result,"删除角色成功");
    	}else{
    		$this->error("删除角色失败");
    	}
    }
    
    /**
     * 排序节点
     * @access public
     * @param  array  $sort 排序数组
     * @return array
     */
    public function sort(){
    
    	$sort   = $_POST["sort"];
    	$model  = D("Node");
    	$result = 0; 
    	foreach($sort as $k=>$v){
    		$result += $model->where("id=".$k)->save(array("sort"=>$v));
    	}
    
    	$this->success($result,"排序成功");
    }
    
	/**
	 * 列出应用系统所有Module
	 * @access public
	 * @param  string  $api  应用系统名称
     * @return array
	 */
	public function listModule(){
		
		$api = $this->_get("api","trim"  ,'hdmin');
		$pid = $this->_get("pid","intval",'pid');
		
		load("@.".$api);
		$model  = D("Node");
		
		$map    = array("level"=>1,"pid"=>$pid);
		$list   = $model->listNode($map);
		$all    = return_hdmin();
		$exsits = $model->filter($list);
		
		$data   = array("all"=>$all, "exsits"=>$exsits);
		$this->success($data);
	}
	
	/**
	 * 列出应用系统所有action
	 * @param  string  $api    应用系统名称
	 * @param  string  $module 应用模块名称
     * @return array
	 */
	public function listAction(){
		
		$api    = $this->_get("api","trim"  ,'hdmin');
		$pid    = $this->_get("pid","intval",0);
		$module = $this->_get("module","trim",'');
		$action = $this->_get("action","trim",'');
		
		load("@.".$api);
		$model  = D("Node");
		$all    = return_hdmin(true,$module);
		if(!$pid){
			
			$map = array("name"=>$module, "level"=>1);
			$pid = $model->where($map)->getField("id");
			
		}else{
			if($action){
				$all = $all[$action]['child'];
			}
		}
		
		$map    = array("pid"=>$pid);//"level"=>2,
		$list   = $model->listNode($map);
		$exsits = $model->filter($list);
		
		$data   = array("all"=>$all, "exsits"=>$exsits);
		$this->success($data);
	}
	
	/**
	 * 根据角色对应的节点
	 * @access public
	 * @param  int    $role 角色id
	 * @return array
	 */
	public function rbacNode(){
		
		$role   = $this->_get("role"  ,"intval",0);
		$userid = $this->_get("userid","intval",0);

		if($role){
			
			$model  = D("NodeRoleView");
			$map1   = array("role"=>$role, "level"=>1);
			$map2['role']  = $role;
			$map2['level'] = array('egt', 2);
			
			$field  = "id,pid,tid,name,title,level,sort,role";
			$order  = "sort desc";
			
			$one    = $model->where($map1)->field($field)->order($order)->select();
			$two    = $model->where($map2)->field($field)->order($order)->select();

			$nodes  = array("one"=>$one, "two"=>$two);
			$this->success($nodes);
		}else{
			$this->error("");
		}
	}
	
	/**
	 * 根据角色对应的菜单
	 * @access public
	 * @param  int    $role   角色id
	 * @param  int    $userid 用户id
	 * @return array
	 */
	public function rbacMenu(){
	
		$role   = $this->_get("role","intval",0);
		$userid = $this->_get("userid","intval",0);
		$field  = "id,nodeid,pid,title,level,sort,url,type,config,icon";
		$order  = "sort desc";
	
		if($role && $userid){
			
			$map    = array("role"=>$role);
			$model  = D("MenuRoleView");
			$menus  = $model->where($map)->field($field)->order($order)->select();
			
			$field  = "id,pid,title,level,url,type,config,orderno,sort,icon";
			$order  = "orderno desc";
			$map    = array("userid"=>$userid);
			$model  = D("QuickUserView");
			$nodes  = $model->where($map)->field($field)->order($order)->select();
			
			$list   = array("menu"=>$menus, "quick"=>$nodes);
			$this->success($list);
		}else{
			$this->error("");	
		}
	}
	
}